<?php
	include_once('header.php');
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		if (isset($_POST['xiugai_submit'])) {
			// start 如果用户点击的是修改按钮，修改分类名称
			$category_name_xiugai = addslashes($_POST['edit_name_category_text']);
			$category_id_xiugai = $_POST['category_id_hidden'];
			$query_edit_category = "update `category` set `name` = '".$category_name_xiugai."' where `id` = ".$category_id_xiugai;
			$mysql_connection->query($query_edit_category);
			// end 如果用户点击的是修改按钮，修改分类名称
		} else if (isset($_POST['shanchu_submit'])) {
			$category_id_shanchu = $_POST['category_id_hidden'];
			$query_del_category = "delete from `category` where `id` = ".$category_id_shanchu;
			$query_del_article_category = "delete from `articles_category` where `category_id`=".$category_id_shanchu;
			$mysql_connection->query($query_del_category);
			$mysql_connection->query($query_del_article_category);
		} else {
			// start 如果用户点击的是添加按钮，添加一个分类
			$new_name_category = addslashes($_POST['name_new_category_text']);
			$query_new_category = "insert into `category` (`name`) values ('".$new_name_category."')";
			$mysql_connection->query($query_new_category);
			// end 如果用户点击的是添加按钮，添加一个分类
		}
	}
?> 
<div id="content_b">
	<!-- start 添加分类表单 -->
	<div id="new_category">
		<form action="<?php echo $_SERVER['PHP_SELF']; ?>?message_box=分类已经添加" method="post">
			分类名称:<input type="text" name="name_new_category_text" id="name_new_category_text" />
			<input type="submit" value="添加" />
		</form>
	</div>
	<!-- end 添加分类表单 -->
	<!-- start 显示分类列表，并可以删除和修改分类 -->
	<div id="list_edit_del_category">
		<table border="1">
			<tr><td>分类号</td><td>分类名称</td><td>修改</td><td>删除</td></tr>
			<?php
				$category_list = select_category($mysql_connection);
				foreach ( $category_list as $category_list_id => $category_list_name ) {
				?>
				<form action="<?php echo $_SERVER['PHP_SELF']; ?>?message_box=操作已完成" method="post">
					<tr><td><?php echo $category_list_id; ?></td>
					<td><input type="text" value="<?php echo $category_list_name; ?>" name="edit_name_category_text" id="edit_name_category_text" /></td>
					<td><input type="submit" value="修改" name="xiugai_submit" id="xiugai_submit" /></td>
					<td><input type="hidden" value="<?php echo $category_list_id; ?>" name="category_id_hidden" id="category_id_hidden" />
					<input type="submit" value="删除" name="shanchu_submit" id="shanchu_submit" onclick="return confirm('确定要删除此分类吗？');" /></td></tr>
				</form>
				<?php
				}
			?>
		</table>
	</div>
	<!-- end 显示分类列表，并可以删除修改分类 -->
</div>
<?php include_once('footer.php'); ?>
